package com.jiumi.baseconfig.service.impl;

import java.util.List;

import com.jiumi.baseconfig.domain.BaseTeacher;
import com.jiumi.baseconfig.mapper.BaseTeacherMapper;
import com.jiumi.common.utils.DateUtils;
import com.jiumi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jiumi.baseconfig.mapper.BaseChapterMapper;
import com.jiumi.baseconfig.domain.BaseChapter;
import com.jiumi.baseconfig.service.IBaseChapterService;

/**
 * 课程目录Service业务层处理
 *
 * @author jiumi
 * @date 2023-05-21
 */
@Service
public class BaseChapterServiceImpl implements IBaseChapterService {
    @Autowired
    private BaseChapterMapper baseChapterMapper;

    @Autowired
    private BaseTeacherMapper baseTeacherMapper;
    /**
     * 查询课程目录
     *
     * @param id 课程目录主键
     * @return 课程目录
     */
    @Override
    public BaseChapter selectBaseChapterById(Long id) {
        return baseChapterMapper.selectBaseChapterById(id);
    }

    /**
     * 查询课程目录列表
     *
     * @param baseChapter 课程目录
     * @return 课程目录
     */
    @Override
    public List<BaseChapter> selectBaseChapterList(BaseChapter baseChapter) {
        return baseChapterMapper.selectBaseChapterList(baseChapter);
    }

    /**
     * 新增课程目录
     *
     * @param baseChapter 课程目录
     * @return 结果
     */
    @Override
    public int insertBaseChapter(BaseChapter baseChapter) {
        baseChapter.setCreateTime(DateUtils.getNowDate());
        updateTeacherName(baseChapter);
        return baseChapterMapper.insertBaseChapter(baseChapter);
    }

    public void updateTeacherName(BaseChapter baseChapter) {
        if (null != baseChapter && StringUtils.isNotNull(baseChapter.getTeacherId())) {
            BaseTeacher baseTeacher = baseTeacherMapper.selectBaseTeacherById(baseChapter.getTeacherId());
            if(StringUtils.isNotNull(baseTeacher)) {
                baseChapter.setTeacherName(baseTeacher.getName());
            }
        }
    }

    /**
     * 修改课程目录
     *
     * @param baseChapter 课程目录
     * @return 结果
     */
    @Override
    public int updateBaseChapter(BaseChapter baseChapter) {
        baseChapter.setUpdateTime(DateUtils.getNowDate());
        updateTeacherName(baseChapter);
        return baseChapterMapper.updateBaseChapter(baseChapter);
    }

    /**
     * 批量删除课程目录
     *
     * @param ids 需要删除的课程目录主键
     * @return 结果
     */
    @Override
    public int deleteBaseChapterByIds(Long[] ids) {
        return baseChapterMapper.deleteBaseChapterByIds(ids);
    }

    /**
     * 删除课程目录信息
     *
     * @param id 课程目录主键
     * @return 结果
     */
    @Override
    public int deleteBaseChapterById(Long id) {
        return baseChapterMapper.deleteBaseChapterById(id);
    }
}
